{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'blue': ['0'], 'red': ['5'], 'green': ['']}\n"
     ]
    }
   ],
   "source": [
    "# Example 1\n",
    "from urllib.parse import parse_qs\n",
    "my_values = parse_qs('red=5&blue=0&green=',\n",
    "                     keep_blank_values=True)\n",
    "print(repr(my_values))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Red:      ['5']\n",
      "Green:    ['']\n",
      "Opacity:  None\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "print('Red:     ', my_values.get('red'))\n",
    "print('Green:   ', my_values.get('green'))\n",
    "print('Opacity: ', my_values.get('opacity'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Red:     '5'\n",
      "Green:   0\n",
      "Opacity: 0\n"
     ]
    }
   ],
   "source": [
    "# Example 3\n",
    "# For query string 'red=5&blue=0&green='\n",
    "red = my_values.get('red', [''])[0] or 0\n",
    "green = my_values.get('green', [''])[0] or 0\n",
    "opacity = my_values.get('opacity', [''])[0] or 0\n",
    "print('Red:     %r' % red)\n",
    "print('Green:   %r' % green)\n",
    "print('Opacity: %r' % opacity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Red:     5\n",
      "Green:   0\n",
      "Opacity: 0\n"
     ]
    }
   ],
   "source": [
    "# Example 4\n",
    "red = int(my_values.get('red', [''])[0] or 0)\n",
    "green = int(my_values.get('green', [''])[0] or 0)\n",
    "opacity = int(my_values.get('opacity', [''])[0] or 0)\n",
    "print('Red:     %r' % red)\n",
    "print('Green:   %r' % green)\n",
    "print('Opacity: %r' % opacity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Red:     5\n",
      "Green:   0\n",
      "Opacity: 0\n"
     ]
    }
   ],
   "source": [
    "# Example 5\n",
    "red = my_values.get('red', [''])\n",
    "red = int(red[0]) if red[0] else 0\n",
    "green = my_values.get('green', [''])\n",
    "green = int(green[0]) if green[0] else 0\n",
    "opacity = my_values.get('opacity', [''])\n",
    "opacity = int(opacity[0]) if opacity[0] else 0\n",
    "print('Red:     %r' % red)\n",
    "print('Green:   %r' % green)\n",
    "print('Opacity: %r' % opacity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Green:   0\n"
     ]
    }
   ],
   "source": [
    "# Example 6\n",
    "green = my_values.get('green', [''])\n",
    "if green[0]:\n",
    "    green = int(green[0])\n",
    "else:\n",
    "    green = 0\n",
    "print('Green:   %r' % green)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Green:   0\n"
     ]
    }
   ],
   "source": [
    "# Example 7\n",
    "def get_first_int(values, key, default=0):\n",
    "    found = values.get(key, [''])\n",
    "    if found[0]:\n",
    "        found = int(found[0])\n",
    "    else:\n",
    "        found = default\n",
    "    return found\n",
    "\n",
    "\n",
    "# Example 8\n",
    "green = get_first_int(my_values, 'green')\n",
    "print('Green:   %r' % green)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
